package com.niyue.sandbox.uclock.clockr;

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.joda.time.DateTime;

import com.niyue.sandbox.uclock.core.Clock;
import com.niyue.sandbox.uclock.core.Event;
import com.niyue.sandbox.uclock.core.SecondTickerEvent;

public class ClockrFace extends JFrame implements Clock{

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private ImagePanel h1 = null;
	
	private ImagePanel h2 = null;
	
	private ImagePanel m1 = null;
	
	private ImagePanel m2 = null;
	
	private ImagePanel s1 = null;
	
	private ImagePanel s2 = null;

	private FlickrDigitPool flickrDigitPool;  //  @jve:decl-index=0:
	
	private int lastH1 = 0;
	
	private int lastH2 = 0;
	
	private int lastM1 = 0;
	
	private int lastM2 = 0;
	
	private int lastS1 = 0;
	
	private int lastS2 = 0;

	public FlickrDigitPool getFlickrDigitPool() {
		return flickrDigitPool;
	}

	public void setFlickrDigitPool(FlickrDigitPool flickrDigitPool) {
		this.flickrDigitPool = flickrDigitPool;
	}

	/**
	 * This is the default constructor.
	 */
	public ClockrFace() {
		super();
		initialize();
		flickrDigitPool=new Clockr(100);		
		LoadingSplash loadingSplash=new LoadingSplash();
		loadingSplash.setFlickrDigitPool(flickrDigitPool);
		loadingSplash.loadPhotos();
		loadingSplash.dispose();
		this.setVisible(true);
	}

	/**
	 * This method initializes this.
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
		this.setTitle("Flickr Alarm");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	/**
	 * This method initializes jContentPane.
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new FlowLayout());
			jContentPane.add(getH1(), null);
			jContentPane.add(getH2(), null);
			jContentPane.add(getM1(), null);
			jContentPane.add(getM2(), null);
			jContentPane.add(getS1(), null);
			jContentPane.add(getS2(), null);
		}
		return jContentPane;
	}



	public ImagePanel getH1() {
		if (h1==null) {
			h1=new ImagePanel();
			h1.setLayout(null);
		}
		return h1;
	}

	public void setH1(ImagePanel h1) {
		this.h1 = h1;
	}

	public ImagePanel getH2() {
		if (h2==null) {
			h2=new ImagePanel();
			h2.setLayout(null);
		}
		return h2;
	}

	public void setH2(ImagePanel h2) {
		this.h2 = h2;
	}

	public ImagePanel getM1() {
		if (m1==null) {
			m1=new ImagePanel();
			m1.setLayout(null);
		}
		return m1;
	}

	public void setM1(ImagePanel m1) {
		this.m1 = m1;
	}

	public ImagePanel getM2() {
		if (m2==null) {
			m2=new ImagePanel();
			m2.setLayout(null);
		}
		return m2;
	}

	public void setM2(ImagePanel m2) {
		this.m2 = m2;
	}

	public ImagePanel getS1() {
		if (s1==null) {
			s1=new ImagePanel();
			s1.setLayout(null);
		}
		return s1;
	}

	public void setS1(ImagePanel s1) {
		this.s1 = s1;
	}

	public ImagePanel getS2() {
		if (s2==null) {
			s2=new ImagePanel();
			s2.setLayout(null);
		}
		return s2;
	}

	public void setS2(ImagePanel s2) {
		this.s2 = s2;
	}

	public void update(Event event) {
		if (event instanceof SecondTickerEvent) {
			SecondTickerEvent tickerEvent = (SecondTickerEvent) event;
			DateTime now=tickerEvent.getTickerTime();
				int hour=now.getHourOfDay();
				int minute=now.getMinuteOfHour();
				int second=now.getSecondOfMinute();
				int h1=hour/10;
				int h2=hour%10;
				int m1=minute/10;
				int m2=minute%10;
				int s1=second/10;
				int s2=second%10;
				if(lastH1 != h1){
					lastH1=h1;
					this.getH1().setImage(flickrDigitPool.getImgesByDigit(h1));
				}
				if(lastH2 != h2){
					lastH2 = h2;
					this.getH2().setImage(flickrDigitPool.getImgesByDigit(h2));
				}
				if(lastM1 != m1){
					lastM1=m1;
					this.getM1().setImage(flickrDigitPool.getImgesByDigit(m1));
				}
				if(lastM2 != m2){
					lastM2 = m2;
					this.getM2().setImage(flickrDigitPool.getImgesByDigit(m2));
				}
				if(lastS1 != s1){
					lastS1=s1;
					this.getS1().setImage(flickrDigitPool.getImgesByDigit(s1));
				}
				if(lastS2 != s2){
					lastS2=s2;
					this.getS2().setImage(flickrDigitPool.getImgesByDigit(s2));
				}
		}					
	}
}
